package com.example.thymeleaf.controller;

import com.example.thymeleaf.bean.Article;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Copyright(C), 2020-2021, AmbroseCdMeng
 * FileName:    ThymeleafController
 * Author:      AmbroseCdMeng
 * Date:        2021/1/1316:53
 * Description: Thymeleaf 视图渲染控制器
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间        版本号           描述
 */
@Controller
public class ThymeleafController {
    // @RequestMapping(value = "index", method = RequestMethod.GET)
    @GetMapping("index")
    public ModelAndView index(HttpServletRequest hsr) {
        ModelAndView mv = new ModelAndView();
        Article article = new Article();
        article.setTitle("来瓶红牛,决战到天亮");
        article.setBody("注意用户体验");
        mv.addObject(article);
        mv.setViewName("index.html");
        return mv;
    }


    @GetMapping("index1")
    public ModelAndView index(Model model) {
        Article article = new Article();
        article.setTitle("运营不要和我说需求");
        article.setBody("想谈需求也可以，扫码改需求");
        model.addAttribute(article);
        return new ModelAndView("index.html");
    }

    /**
     * 获取 url 中的参数
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
    public Model getModel(@PathVariable String id, Model model) {
        //
        return null;
    }

    /**
     * 获取 body 中的参数
     *
     * @param model
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.POST, consumes = "application/json")
    public void addModel(@RequestBody Model model) {
        //
    }

    /**
     * 获取路径中的参数
     *
     * @param id
     * @return
     */
    @GetMapping("article/{id}")
    public ModelAndView getArticle(@PathVariable("id") Integer id) {
        Article article = new Article();
        ModelAndView mv = new ModelAndView("index.html");
        mv.addObject(article);
        return mv;
    }

    /**
     * 通过 Bean 接收 HTTP 对象
     *
     * @param article
     * @return
     */
    @PostMapping("/add")
    public String addArticle1(Article article) {
        return null;
    }

    /**
     * 用注解 @ModelAttribute 获取参数
     *
     * @param article
     * @return
     */
    @RequestMapping(value = "/add/article", method = RequestMethod.POST)
    public String addArticle2(@ModelAttribute("article") Article article) {
        return null;
    }

    /**
     * 通过 RequestParams 绑定入参，可设定默认值，当请求参数不存在时会有异常发生，可设置 required = false 避免
     *
     * @param title
     * @return
     */
    @RequestMapping(value = "add/article", method = RequestMethod.POST)
    public String addArticle3(@RequestParam(value = "title", defaultValue = "unknow", required = false) String title) {
        return null;
    }

    /**
     * 通过 RequestBody 接收 JSON 数据
     *
     * @param articleList
     * @return
     */
    @RequestMapping("/update")
    public String updateArticle(@RequestBody List<Article> articleList) {
        return null;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param redirect
     * @return
     */
    @RequestMapping("/upload")
    public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirect) {
        if (file.isEmpty()) {
            redirect.addFlashAttribute("message", "请选择文件");
            return "redirect:uploadStatus";
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get("" + file.getOriginalFilename());
            Files.write(path, bytes);
            redirect.addFlashAttribute("message", "上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return "redirect:/uploadStatus";
    }
}
